package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Area;
import com.wm.bean.City;
import com.wm.bean.MenuType;
import com.wm.comm.Constant;
import com.wm.service.AreaService;
import com.wm.service.CityService;
import com.wm.service.MenuTypeService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.ui.OptionsInt;
import com.wm.web.ui.OptionsLong;

public class AdminMenuType extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminMenuType.class);
	
	private MenuTypeService menuTypeService;

	public MenuTypeService getMenuTypeService() {
		return menuTypeService;
	}

	public void setMenuTypeService(MenuTypeService menuTypeService) {
		this.menuTypeService = menuTypeService;
	}


	private List isShowList = new ArrayList();

	public List getIsShowList() {
		return isShowList;
	}

	public void setIsShowList(List isShowList) {
		this.isShowList = isShowList;
	}
	
	private void setIsShowListValues(){
		isShowList = Constant.IS_SHOW;
	}
	
	private void initValues(){
		setIsShowListValues();
	}
	
	private void listInitValues(){
		setIsShowListValues();
	}


	private long id;					//主键ID
	private String typeName;			//菜单分类名称
	private String typeMemo;			//备注
	private int isShow = -1;			//是否显示
	private int orderNum;				//排序字段
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public String getTypeMemo() {
		return typeMemo;
	}

	public void setTypeMemo(String typeMemo) {
		this.typeMemo = typeMemo;
	}

	public int getIsShow() {
		return isShow;
	}

	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}

	public int getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(int orderNum) {
		this.orderNum = orderNum;
	}

	private List typeList;

	public List getTypeList() {
		return typeList;
	}

	public void setTypeList(List typeList) {
		this.typeList = typeList;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		listInitValues();
		this.setTypeList(this.getMenuTypeService().findMenuTypeByIsShow(this.isShow, "id", 0));
		return SUCCESS;
	}
	
	public String add(){
		initValues();
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		//添加
		MenuType menuType = new MenuType();
		menuType.setTypeName(this.typeName);
		menuType.setTypeMemo(this.typeMemo);
		menuType.setIsShow(this.isShow);
		menuType.setOrderNum(this.orderNum);
		menuType.setOrderTime(new Date());
		menuType.setAddTime(new Date());
		try {
			this.getMenuTypeService().saveMenuType(menuType);
		} catch (Exception e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		initValues();
		this.setAction("doupdate");
		MenuType menuType = this.getMenuTypeService().findMenuTypeById(this.id);
		this.setTypeName(menuType.getTypeName());
		this.setTypeMemo(menuType.getTypeMemo());
		this.setIsShow(menuType.getIsShow());
		this.setOrderNum(menuType.getOrderNum());
		return INPUT;
	}
	
	public String doupdate(){
		//添加
		MenuType menuType = this.getMenuTypeService().findMenuTypeById(this.id);
		menuType.setTypeName(this.typeName);
		menuType.setTypeMemo(this.typeMemo);
		menuType.setIsShow(this.isShow);
		menuType.setOrderNum(this.orderNum);
		menuType.setOrderTime(new Date());
		menuType.setAddTime(new Date());
		try {
			this.getMenuTypeService().updateMenuType(menuType);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String orders(){
		MenuType menuType = this.getMenuTypeService().findMenuTypeById(this.id);
		menuType.setOrderTime(new Date());
		try {
			this.getMenuTypeService().updateMenuType(menuType);
		} catch (Exception e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getMenuTypeService().removeMenuTypeById(this.id);
		return "index";
	}
	
	
}
